package com.xb.controller;

import com.xb.entity.Article;
import com.xb.entity.Result;
import com.xb.entity.User;
import com.xb.service.ArticleService;
import com.xb.service.UserService;
import com.xb.websocket.XBWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.xml.ws.Service;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private HttpSession session;

    @Autowired
    private UserService userService;

    @Autowired
    private ArticleService articleService;

    /**
     * 文章列表
     * @param page 当前页
     * @param search 文章标题搜索
     * @return 文章列表
     */
    @RequestMapping("/search")
    public Result findAll(Integer page, String search){
        return new Result(true, "查询成功",articleService.findArticle(search,page));
    }

    /**
     * 文章详情
     * @param articleId 文章ID
     * @return 文章信息
     */
    @RequestMapping("/detail/{articleId}")
    public Result detail(@PathVariable Long articleId){
        String username = (String) session.getAttribute("loginUser");
        User loginUser = userService.findByUsername(username);
        Map<String, Object> map = articleService.updateArticle(loginUser.getId(), articleId);
        return new Result(true,"查询成功", map);
    }

    /**
     * 收藏/取消
     * @param id 文章ID
     * @param state 收藏状态
     * @return 处理结果
     */
    @RequestMapping("/changeFavorite")
    public Result changeFavorite(Long id, Boolean state){
        String username = (String) session.getAttribute("loginUser");
        User loginUser = userService.findByUsername(username);
        // 先查询我是否收藏过这篇文章
        Boolean isFavorite = articleService.isFavorite(loginUser.getId(), id);
        if (isFavorite) {
            // 来取消收藏的
            articleService.changFavorite(loginUser.getId(), id, false);
            Integer favoriteCount = articleService.countFavoriteByArticleId(id);
            return new Result(false, "取消收藏成功", favoriteCount);
        } else {

            // 来收藏的
            articleService.changFavorite(loginUser.getId(), id, true);
            Integer favoriteCount = articleService.countFavoriteByArticleId(id);

            // 查询这篇文章是谁发布的
            Long userId = articleService.findById(id).getUserId();

            // 给这个人推送信息
            XBWebSocket.sendMessage(userId,loginUser.getRealName()+"刚刚收藏了您的文章！");

            return new Result(true, "收藏成功", favoriteCount);
        }
    }


    @RequestMapping("/favoriteArticle")
    public Result favoriteArticle(Integer page, String search){
        String username = (String) session.getAttribute("loginUser");
        User loginUser = userService.findByUsername(username);
        return new Result(true, "查询成功", articleService.findMyFavorite(loginUser.getId(), search, page));
    }

    @RequestMapping("/addArticle")
    public Result addArticle(@RequestBody Article article){
        String username = (String) session.getAttribute("loginUser");
        User loginUser = userService.findByUsername(username);
        articleService.addArticle(loginUser, article);
        return new Result(true, "添加成功");
    }
}
